<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-9-16
 * Time: 上午11:14
 */
/*接受数据*/
$name      = $_GET["name"];//uu_land的id
$endtime   = $_GET['endtime'];
$begintiem = $_GET['begintiem'];
/*$target标志位，分为三个值，1代表搜索原始供应商，2代表搜索显示一级分销商，3代表搜索显示末级分销商*/
$target    = $_GET['target'];
strtotime($begintiem)>time()?exit('{"status":"fail","msg":"请选择正确时间"}'):'';
$begintiem==date('Y-m-d',time())?exit('{"status":"fail","msg":"当天时间没有数据"}'):'';
$p_type    = array('A'=>'景区','B'=>'路线','C'=>'酒店','F'=>'套票','G'=>'餐饮','H'=>'演出');
/*连接数据库*/
require_once("/var/www/html/new/conf/le.je");
$le        = new go_sql();
$le->connect();
/*
 * 输入产品名称后再通过搜索数据库，查询是否存在此数据，不存在则跳出
 * 本来是通过景区的id直接来搜索是否存在该id，但是有的景区会发布两个
 * 相同名称的产品，则有可能导致数据的错误，所以通过传入产品的名称将
 * 改产品的id全部搜索出来
*/
if(!empty($name)) {
    /*通过输入的产品名称搜索数据库得到产品的信息*/
    $sql= "SELECT id,title FROM uu_land WHERE title='$name'";
    $GLOBALS['le']->query($sql);
    /*搜索显示景区的信息，键值为景区id即lid*/
    while($row=$GLOBALS['le']->fetch_assoc()) {
        $lid_fid.=$row['id'].',';
    }
    $lid_fid=rtrim($lid_fid,',');
    /*判断是否能找到此产品，找不到则跳出*/
    empty($lid_fid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    $sql ="SELECT id,landid FROM uu_jq_ticket WHERE landid in($lid_fid)";//搜索票的id及tid
    $GLOBALS['le']->query($sql);
    while($row = $GLOBALS['le']->fetch_assoc()) {
        $tid .= $row['id'].',';
    }
    $tid=rtrim($tid,',');
}
$var=array(
    'tid'      =>$tid,
    'target'   =>$target,
    'endtime'  =>$endtime,
    'begintiem'=>$begintiem
);
/*连接另外一个服务器，搜索数据*/
$return_tid  = curr(array_filter($var));
$return_tid  = json_decode($return_tid,true);
empty($return_tid['tid'])?exit('{"status":"fail","msg":"此产品在此期间未购买物品"}'):'';
/*搜索景区的id*/
$sql_lid="select id,landid from uu_jq_ticket where id in ({$return_tid['tid']})";//tid联系在一起
$GLOBALS['le']->query($sql_lid);
$r_tid=$scenic=$dname=$return=array();
while($row = $GLOBALS['le']->fetch_assoc()) {
    $r_tid[$row['id']] =$row['landid'];
    $lid_str .= $row['landid'].',';
}
$lid_str=trim($lid_str,',');
/*搜索景区的类型*/
$sql_type="select id,p_type,title,jtype from uu_land where id in ({$lid_str})";
$GLOBALS['le'] ->query($sql_type);
while($row = $GLOBALS['le']->fetch_assoc()) {
    foreach($r_tid as $tid_key =>$tid_val) {
      if($tid_val==$row['id']) {   /*tid联系起来*/
          $scenic[$tid_key]=array(
              'p_type'=>$p_type[$row['p_type']],
              'title' =>$row['title'],
              'jtype' =>$row['jtype'],
              'lid'   =>$row['id']
          );
      }
    }
}
/*
 *
 * 搜索原始分销商、初级分销商及末级分销商，从另一个服务器得到的数据是不一样的，所以分情况显示出来
 *
*/
switch($target) {
    /*原始供应商*/
    case 1:
        $sql_dname="select id,dname from pft_member where id in ({$return_tid['aid']})";
        $GLOBALS['le']->query($sql_dname);
        while($row = $GLOBALS['le']->fetch_assoc()) {
            $dname[$row['id']] = $row['dname'];
        }
        /*删除无用数据*/
        unset($return_tid['tid'],$return_tid['aid']);
        foreach($return_tid as $return_key => $return_val) {   /*返回数据*/
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['tnums']   +=$return_val['tnum'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['moneys']  +=$return_val['money'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['torders'] +=$return_val['torder'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['title']  =$scenic[$return_val['tid']]['title'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['jtype'] =$scenic[$return_val['tid']]['jtype'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['p_type'] =$scenic[$return_val['tid']]['p_type'];
            $return[(int)$return_val['aid'].$scenic[$return_val['tid']]['lid']]['dname'] =$dname[$return_val['aid']];
        }
        unset($scenic,$dname);
        echo json_encode($return);
        break;
    /*初级分销商*/
    case 2:
        $sql_dname="select id,dname from pft_member where id in ({$return_tid['mid']})";
        $GLOBALS['le']->query($sql_dname);
        while($row = $GLOBALS['le']->fetch_assoc()) {
            $dname[$row['id']] = $row['dname'];
        }
        /*删除无用数据*/
        unset($return_tid['tid'],$return_tid['mid']);
        foreach($return_tid as $return_key => $return_val) {   /*返回数据*/
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['tnums']   +=$return_val['tnum'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['moneys']  +=$return_val['money'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['torders'] +=$return_val['torder'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['title']  =$scenic[$return_val['tid']]['title'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['jtype'] =$scenic[$return_val['tid']]['jtype'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['p_type'] =$scenic[$return_val['tid']]['p_type'];
            $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['dname'] =$dname[$return_val['mid']];
        }
        unset($scenic,$dname);
        echo json_encode($return);
        break;
    /*末级分销商*/
    case 3:
        $sql_dname="select id,dname from pft_member where id in ({$return_tid['mid']})";
        $GLOBALS['le']->query($sql_dname);
        while($row = $GLOBALS['le']->fetch_assoc()) {
            $dname[$row['id']] = $row['dname'];
        }
        /*删除无用数据*/
        unset($return_tid['tid'],$return_tid['mid']);
        foreach($return_tid as $return_key => $return_va) {
        /*返回数据*/
            foreach($return_va as $return_val) {
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['tnums']   +=$return_val['tnum'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['moneys']  +=$return_val['money'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['torders'] +=$return_val['torder'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['title']  =$scenic[$return_val['tid']]['title'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['jtype'] =$scenic[$return_val['tid']]['jtype'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['p_type'] =$scenic[$return_val['tid']]['p_type'];
                $return[(int)$return_val['mid'].$scenic[$return_val['tid']]['lid']]['dname'] =$dname[$return_val['mid']];
            }
        }
        unset($scenic,$dname);
        echo json_encode($return);
        break;
}
function curr($query) {
    //$query['token'] = md5($query['btime'].'7667659191929397103470329788807862402627565924638280479623896');
    $url = "http://s.12301.cc/pft/order_report/distribution.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return json_decode(curl_exec($ch),true);
    return curl_exec($ch);
}
